#include"./http.hpp"

#define WWW "./wwwroot"

std::string RequestString(const HttpRequest &req) {
    std::stringstream str;
    str<<req._method<<" "<<req._path<<" "<<req._version<<"\r\n";
    for(auto &it:req._params){
        str<<it.first<<": "<<it.second<<"\r\n";
    }
    for(auto &it:req._headers){
        str<<it.first<<": "<<it.second<<"\r\n";
    }
    str<<"\r\n";
    str<<req._body;
    return str.str();
}

void Hello(const HttpRequest &req, HttpReponse *res){
    res->SetContent(RequestString(req), "text/plain");
    // sleep(15);
}

void Login(const HttpRequest &req, HttpReponse *res){
    res->SetContent(RequestString(req), "text/plain");
}

void Put(const HttpRequest &req, HttpReponse *res){
    //  res->SetContent(RequestString(req), "text/plain");
    std::string path = WWW + req._path;
    Util::WriteFile(path, req._body);
}

void Delete(const HttpRequest &req, HttpReponse *res){
    res->SetContent(RequestString(req), "text/plain");
}

int main()
{
    HttpServer server(8080);
    server.SetThreadCount(3);
    server.SetBaseDir(WWW);//设置静态资源目录，告诉服务器有静态资源请求的时候去哪里找 
    server.Get("/hello",Hello);
    server.Post("/login",Login);
    server.Put("/put",Put);
    server.Delete("/delete",Delete);
    server.Listen();
    return 0;
}